local cjson         = require('cjson.safe')
local DB            = require("libs.db")

local db = DB:new()


local getAllUser = function()
    local res, err =  db:query("SELECT id,name,sex FROM user")
    if res == nil then
        res = {}
    end
    return res
end



local res1 = getAllUser()

ngx.sleep(1)
local getAllSkill = function()
    local res, err =  db:query("SELECT id,userid,skill FROM skill")
    if res == nil then
        res = {}
    end
    return res
end




local res2 = getAllSkill()
ngx.sleep(1)

local result = {}

for i=1,#res1 do
    if result[res1[i]["name"]] == nil then
        result[res1[i]["name"]] = {}
    end
    for j=1,#res2 do
        if res1[i]["id"] == res2[j]["userid"] then
            result[res1[i]["name"]][#result[res1[i]["name"]]+1] = res2[j]["skill"]
        end
    end
end


result =  cjson.encode(result)
ngx.say(result)
return 

